package com.learn.eduadboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.learn.eduadboot.mapper.PromotionSpaceDao;
import com.learn.eduadboot.service.PromotionSpaceService;
import com.learn.eduadboot.util.RedisUtils;
import com.learn.eduapi.entity.ad.PromotionSpace;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PromotionSpaceServiceImpl implements PromotionSpaceService {
    @Autowired
    private PromotionSpaceDao promotionSpaceDao;
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public int removeAdSpace(Integer id) {
        return promotionSpaceDao.deleteById(id);
    }

    @Override
    public int addSpace(PromotionSpace promotionSpace) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(date);
        promotionSpace.setCreateTime(dateString);
        promotionSpace.setUpdateTime(dateString);
        promotionSpace.setIsDel(0);
        //清楚Redis缓存
        if (redisUtils.get("adList")!=null){
            redisUtils.remove("adList");
        }
        return promotionSpaceDao.insert(promotionSpace);
    }

    @Override
    public PromotionSpace selectBy(String name, String spaceKey) {
        return promotionSpaceDao.selectBy(name, spaceKey);
    }

    @Override
    public int updatePromotionSpace(PromotionSpace promotionSpace, Wrapper<PromotionSpace> wrapper) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(date);
        promotionSpace.setUpdateTime(dateString);
        //清楚Redis缓存
        if (redisUtils.get("adList")!=null){
            redisUtils.remove("adList");
        }
        return promotionSpaceDao.update(promotionSpace,wrapper);
    }

    @Override
    public List<PromotionSpace> getAllPromotionSpace(@Param("find")String find, @Param("page")Integer page, @Param("size")Integer size) {
        return promotionSpaceDao.getAllPromotionSpace(find, page, size);
    }
}
